//
//  WJPDFDrawSignViewController.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/23.
//

import UIKit
import SnapKit

protocol WJPDFDrawSignViewControllerDelegate: AnyObject {
    func didCreatePdfSign(_ controller: WJPDFDrawSignViewController, signatureImage: UIImage)
}

class WJPDFDrawSignViewController: WJBaseViewViewController {
    
    weak var delegate: WJPDFDrawSignViewControllerDelegate? = nil
    
    lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#121212", alpha: 0.05)
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 24
        view.layer.borderWidth = 1.0
        view.layer.borderColor = UIColor(hex: "#FFFFFF", alpha: 0.12).cgColor
        return view
    }()
    
    // 签名绘制视图
    private lazy var drawView: WJPDFSignatureDrawView = {
        let view = WJPDFSignatureDrawView()
        view.layer.cornerRadius = 12
        view.clipsToBounds = true
        view.layer.borderColor = UIColor(hex: "#121212", alpha: 0.05).cgColor
        view.layer.borderWidth = 1
        return view
    }()
    
    // 创建按钮
    private lazy var createButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Create", for: .normal)
        button.backgroundColor = UIColor(hex: "#1F87F6")
        button.setTitleColor(UIColor(hex: "#FFFFFF"), for: .normal)
        button.layer.cornerRadius = 22
        button.titleLabel?.font = UIFont.montserratRegular(16)
        button.addTarget(self, action: #selector(createButtonTapped), for: .touchUpInside)
        return button
    }()
    
    // 橡皮擦按钮
    private lazy var eraserButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "Icon_fill_eraser_selected"), for: .normal)
        button.setImage(UIImage(named: "Icon_fill_eraser_selected"), for: .selected)
        button.backgroundColor = .clear
        button.isSelected = false
        button.alpha = 0.5
        button.addTarget(self, action: #selector(eraserButtonTapped(_:)), for: .touchUpInside)
        return button
    }()
    
    private lazy var drawLabel: UILabel = {
        let label = UILabel()
        label.text = "Draw your signature"
        label.font = UIFont.montserratMedium(16)
        label.textColor = UIColor(hex: "#000000")
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupConstraints()
    }
    
    private func setupUI() {
        // 导航栏设置
        navigationItem.title = "Draw Sign"

        // 添加视图
        view.addSubview(contentView)
        contentView.addSubview(drawView)
        contentView.addSubview(drawLabel)
        contentView.addSubview(eraserButton)
        view.addSubview(createButton)
    }
    
    private func setupConstraints() {
        
        // 创建按钮约束
        createButton.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-20 - safeAreaBottom)
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
            make.width.equalTo(247)
        }
        
        contentView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(30)
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(createButton.snp.top).offset(-25)
        }
        
        // 绘制视图约束
        drawView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(40)
            make.leading.trailing.bottom.equalToSuperview().inset(12)
        }
        
        // 绘制标签约束
        drawLabel.snp.makeConstraints { make in
            make.bottom.equalTo(drawView.snp.top).offset(-10)
            make.leading.equalTo(drawView)
        }
        
        // 橡皮擦按钮约束
        eraserButton.snp.makeConstraints { make in
            make.top.equalTo(drawView).offset(5)
            make.trailing.equalTo(drawView).offset(-5)
            make.width.height.equalTo(50)
        }
    }
    
    @objc private func createButtonTapped() {

        // 获取签名图片
        if let signatureImage = drawView.generateSignatureImage() {
            guard !signatureImage.isImageFullyTransparent() else {
                self.navigationController?.popViewController(animated: true)
                return
            }
            CATransaction.begin()
            CATransaction.setCompletionBlock { [weak self] in
                guard let self = self else { return }
                self.delegate?.didCreatePdfSign(self, signatureImage: signatureImage)
            }
            self.navigationController?.popViewController(animated: true)
            CATransaction.commit()
        } else {
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    @objc private func eraserButtonTapped(_ button: UIButton) {
        button.isSelected = !button.isSelected
        // 切换橡皮擦模式
        drawView.isEraserMode = button.isSelected
        button.alpha = button.isSelected ? 1.0 : 0.5
    }
}
